package com.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.dao.TCatelogDAO;
import com.dao.TGoodsDAO;
import com.dao.TJinhuoDAO;
import com.dao.TOrderItemDAO;
import com.model.TGoods;
import com.model.TJinhuo;
import com.model.TOrderItem;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class goodsAction extends ActionSupport
{
	private Integer goodsId;
	private Integer goodsCatelogId;
	private String goodsName;
	private String goodsMiaoshu;
	
	private String fujian;
	private Integer goodsShichangjia;
	private String goodsIsnottejia;
	private Integer goodsTejia;
	
	private String goodsDel;
	
	private String message;
	private String path;
	
	private TGoodsDAO goodsDAO;
	private TCatelogDAO catelogDAO;
	private TOrderItemDAO orderItemDAO;
	private TJinhuoDAO jinhuoDAO;
	
	public String goodsAdd()
	{
		TGoods goods=new TGoods();
		
		//goods.setGoodsId(goodsId);
		goods.setGoodsCatelogId(goodsCatelogId);
		goods.setGoodsName(goodsName);
		goods.setGoodsMiaoshu(goodsMiaoshu);
		
		goods.setGoodsPic((fujian.equals("")?"/img/zanwu.jpg":fujian));
		goods.setGoodsShichangjia(goodsShichangjia);
		goods.setGoodsIsnottejia("no");
		goods.setGoodsTejia(goodsShichangjia);
		
		goods.setGoodsDel("no");
		
		goodsDAO.save(goods);
		this.setMessage("添加成功");
		this.setPath("goodsMana.action");
		return "succeed";
		
	}
	
	
	public String goodsMana()
	{
		String sql="from TGoods where goodsDel='no' order by goodsIsnottejia";
		List goodsList=goodsDAO.getHibernateTemplate().find(sql);
		for(int i=0;i<goodsList.size();i++)
		{
			TGoods goods=(TGoods)goodsList.get(i);
			goods.setCatelog(catelogDAO.findById(goods.getGoodsCatelogId()));
			goods.setKucun(get_kucun(goods.getGoodsId()));
		}
		Map request=(Map)ServletActionContext.getContext().get("request");
		request.put("goodsList", goodsList);
		return ActionSupport.SUCCESS;
	}
	
	
	public String goodsDel()
	{
		TGoods goods=goodsDAO.findById(goodsId);
		goods.setGoodsDel("yes");
		goodsDAO.attachDirty(goods);
		this.setMessage("删除成功");
		this.setPath("goodsMana.action");
		return "succeed";
	}
	
	public String goodsDetailHou()
	{
		Map request=(Map)ServletActionContext.getContext().get("request");
		
		TGoods goods=goodsDAO.findById(goodsId);
		request.put("goods", goods);
		return ActionSupport.SUCCESS;
	}
	
	public String goodsPre()
	{
		TGoods goods=goodsDAO.findById(goodsId);
		Map request=(Map)ServletActionContext.getContext().get("request");
		request.put("goods", goods);
		return ActionSupport.SUCCESS;
	}
	
	
	public String goodsEdit()
	{
		TGoods goods=goodsDAO.findById(goodsId);
		
		//goods.setGoodsId(goodsId);
		//goods.setGoodsCatelogId(goodsCatelogId);
		goods.setGoodsName(goodsName);
		goods.setGoodsMiaoshu(goodsMiaoshu);
		
		goods.setGoodsPic((fujian.equals("")?"/img/zanwu.jpg":fujian));
		goods.setGoodsShichangjia(goodsShichangjia);
		goods.setGoodsIsnottejia("no");
		goods.setGoodsTejia(goodsShichangjia);
		
		goods.setGoodsDel("no");
		
		goodsDAO.attachDirty(goods);
		this.setMessage("编辑成功");
		this.setPath("goodsMana.action");
		return "succeed";
	}
	
	
	
	
	
	public String goodsByCatelog()
	{
        Map request=(Map)ServletActionContext.getContext().get("request");
		
		String sql="from TGoods where goodsDel='no' and goodsCatelogId=? order by goodsCatelogId";
		Object[] con={goodsCatelogId};
		List goodsList=goodsDAO.getHibernateTemplate().find(sql,con);
		request.put("goodsList", goodsList);
		
		return ActionSupport.SUCCESS;
	}
	
	
	public String goodsDetailQian()
	{
		Map request=(Map)ServletActionContext.getContext().get("request");
		
		TGoods goods=goodsDAO.findById(goodsId);
		request.put("goods", goods);
		return ActionSupport.SUCCESS;
	}
	
	
	public String goodsRes()
	{
        Map request=(Map)ServletActionContext.getContext().get("request");
        Map session=ActionContext.getContext().getSession();
		
		String sql="from TGoods where goodsDel='no' and goodsName like '%"+goodsName.trim()+"%'";
		
		List goodsList=goodsDAO.getHibernateTemplate().find(sql);
		request.put("goodsList", goodsList);
		
		Object o=session.get("userType");
		
		if(o==null)
		{
			System.out.println("û�е�¼");
		}
		else
		{
			
				
		}
		
		return ActionSupport.SUCCESS;
	}
	
	public int get_kucun(int goodsId)
	{
        int j=0;
		String sql="from TJinhuo where goods_id="+goodsId;
		List jinhuoList=jinhuoDAO.getHibernateTemplate().find(sql);
		for(int i=0;i<jinhuoList.size();i++)
		{
			TJinhuo jinhuo=(TJinhuo)jinhuoList.get(i);
			j=j+jinhuo.getShuliang();
		}
		
        int k=0;
		String sql1="from TOrderItem where goodsId="+goodsId;
		List orderItemList=orderItemDAO.getHibernateTemplate().find(sql1);
		for(int i=0;i<orderItemList.size();i++)
		{
			TOrderItem orderItem=(TOrderItem)orderItemList.get(i);
			k=k+orderItem.getGoodsQuantity();
		}
		
		return j-k;
	}
	
	public Integer getGoodsId()
	{
		return goodsId;
	}

	public void setGoodsId(Integer goodsId)
	{
		this.goodsId = goodsId;
	}

	public String getGoodsName()
	{
		return goodsName;
	}

	public void setGoodsName(String goodsName)
	{
		this.goodsName = goodsName;
	}

	public String getGoodsMiaoshu()
	{
		return goodsMiaoshu;
	}

	public void setGoodsMiaoshu(String goodsMiaoshu)
	{
		this.goodsMiaoshu = goodsMiaoshu;
	}

	public String getFujian()
	{
		return fujian;
	}


	public void setFujian(String fujian)
	{
		this.fujian = fujian;
	}

	public Integer getGoodsShichangjia()
	{
		return goodsShichangjia;
	}

	public void setGoodsShichangjia(Integer goodsShichangjia)
	{
		this.goodsShichangjia = goodsShichangjia;
	}

	public Integer getGoodsTejia()
	{
		return goodsTejia;
	}

	public void setGoodsTejia(Integer goodsTejia)
	{
		this.goodsTejia = goodsTejia;
	}

	public String getGoodsIsnottejia()
	{
		return goodsIsnottejia;
	}

	public void setGoodsIsnottejia(String goodsIsnottejia)
	{
		this.goodsIsnottejia = goodsIsnottejia;
	}


	public Integer getGoodsCatelogId()
	{
		return goodsCatelogId;
	}

	public void setGoodsCatelogId(Integer goodsCatelogId)
	{
		this.goodsCatelogId = goodsCatelogId;
	}

	public String getGoodsDel()
	{
		return goodsDel;
	}

	public void setGoodsDel(String goodsDel)
	{
		this.goodsDel = goodsDel;
	}

	public String getMessage()
	{
		return message;
	}

	public void setMessage(String message)
	{
		this.message = message;
	}

	public String getPath()
	{
		return path;
	}

	public void setPath(String path)
	{
		this.path = path;
	}

	public TGoodsDAO getGoodsDAO()
	{
		return goodsDAO;
	}

	public void setGoodsDAO(TGoodsDAO goodsDAO)
	{
		this.goodsDAO = goodsDAO;
	}

	public TCatelogDAO getCatelogDAO()
	{
		return catelogDAO;
	}

	public void setCatelogDAO(TCatelogDAO catelogDAO)
	{
		this.catelogDAO = catelogDAO;
	}

	public TOrderItemDAO getOrderItemDAO() {
		return orderItemDAO;
	}

	public void setOrderItemDAO(TOrderItemDAO orderItemDAO) {
		this.orderItemDAO = orderItemDAO;
	}

	public TJinhuoDAO getJinhuoDAO() {
		return jinhuoDAO;
	}

	public void setJinhuoDAO(TJinhuoDAO jinhuoDAO) {
		this.jinhuoDAO = jinhuoDAO;
	}
	
}
